package br.com.CF.activitys;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import br.com.CF.R;
import br.com.CF.dao.ContaDao;
import br.com.CF.entidades.Conta;

public class AddContaActivity extends Activity {

	private ContaDao dao;
	private Conta conta;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_conta_layout);

		Button saveButton = (Button)findViewById(R.id.btn_adicionar_conta);
		Button sairButton = (Button)findViewById(R.id.btn_termino);
		final EditText nmConta = (EditText)findViewById(R.id.nm_conta);
		final EditText descConta = (EditText)findViewById(R.id.desc_conta);
		final EditText saldo = (EditText)findViewById(R.id.saldo_conta);

		saveButton.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				try{
					createConta(nmConta.getEditableText().toString(), descConta.getEditableText().toString(), Double.parseDouble(saldo.getEditableText().toString()));
					nmConta.setText("");
					descConta.setText("");
					saldo.setText("");
					Toast.makeText(AddContaActivity.this, "Conta "+conta.getNm_conta()+" adicionada com sucesso!", Toast.LENGTH_SHORT).show();;

				}catch(Exception e){
					Toast.makeText(AddContaActivity.this, "Erro ao adicionar Conta!", Toast.LENGTH_SHORT).show();
				}
			}
		});

		sairButton.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				finish();				
			}
		});
	}

	public void createConta(String nm_conta, String desc_conta, Double saldo) {
		dao = new ContaDao(this);

		conta = new Conta();
		conta.setNm_conta(nm_conta);
		conta.setDesc_conta(desc_conta);
		conta.setSaldo(saldo);
		dao.createConta(conta);
	}
}
